package com.compass.doctor.ui.mine

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import com.compass.doctor.base.BaseVBActivity
import com.compass.doctor.databinding.ActivityAddBankcardBinding
import com.compass.doctor.ui.mine.viewmodel.IntegralViewModel
import com.compass.framework.ext.onClick
import com.compass.framework.model.AddBankCard

/**
 * Description： 添加银行卡
 * Date:2024/1/29 17:20
 */
class AddBankCardActivity : BaseVBActivity<ActivityAddBankcardBinding>() {

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, AddBankCardActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val integralViewModel: IntegralViewModel by viewModels()
    private lateinit var cardholderName: String
    private lateinit var bankCardNumber: String
    private lateinit var bankName: String
    private lateinit var openingBranch: String
    private lateinit var mobile: String
    override fun initView(savedInstanceState: Bundle?) {

        mBinding.tvSave.onClick {
            setBankCardInfo()
        }
    }

    override fun initData() {
        integralViewModel.addCardLiveData.observe(this) {
            if (it) {
                setResult(RESULT_OK, intent)
                finish()
            }
        }

    }

    private fun setBankCardInfo() {
        mBinding.edtUserName.text?.let {
            cardholderName = it.toString().trim()
        }
        mBinding.edtCardNumber.text?.let {
            bankCardNumber = it.toString().trim()
        }
        mBinding.edtOpeningBank.text?.let {
            bankName = it.toString().trim()
        }
        mBinding.edtSubBank.text?.let {
            openingBranch = it.toString().trim()
        }
        mBinding.edtPhone.text?.let {
            mobile = it.toString().trim()
        }
        if (cardholderName.isEmpty()) {
            showToast("请输入持卡人姓名")
            return
        }
        if (bankCardNumber.isEmpty()) {
            showToast("请输入银行卡号")
            return
        }

        if (bankName.isEmpty()) {
            showToast("请输入开户行")
            return
        }
        if (openingBranch.isEmpty()) {
            showToast("请输入开户支行")
            return
        }
        if (mobile.isEmpty()) {
            showToast("请输入手机号码")
            return
        }
        var addBankCard =
            AddBankCard(
                cardholderName, bankCardNumber,
                bankName, openingBranch, mobile
            )
        integralViewModel.addBankCard(addBankCard)
    }
}